package es.android.alertes.client;

import es.android.alertes.R;
import es.android.alertes.objects.AlertaEntity;
import greendroid.app.GDMapActivity;
import greendroid.graphics.drawable.DrawableStateSet;
import greendroid.graphics.drawable.MapPinDrawable;

import java.util.ArrayList;
import java.util.List;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.dataframework.DataFramework;
import com.android.dataframework.Entity;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class Mapctivity extends GDMapActivity {

    private static final int[] PRESSED_STATE = {
        android.R.attr.state_pressed
    };
    
	private String CODI = "N1A3";
	private static String TABLE = "alerta";
	
	private TextView txtNom;
	private TextView txtDetalle;
	private ProgressBar estrellas;
	private MapView mapa;
	
	private ArrayList<AlertaEntity> alertas;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActionBarContentView(R.layout.main);
        
        try
        {
        	DataFramework.getInstance().open(this, "com.alorma.bcn");
        } catch (Exception e){
        	e.printStackTrace();
        }
        
        Bundle extras = getIntent().getExtras();
        if (extras.containsKey("CODIGO"))
        {
        	CODI = extras.getString("CODIGO");
        }

        findViews();
        
        String where = "codi = \"" + CODI + "\"";
        List<Entity> entidades = DataFramework.getInstance().getEntityList(TABLE, where);
        
        alertas = new ArrayList<AlertaEntity>();
        
        for (int i = 0; i < entidades.size(); i++)
        {
        	alertas.add(new AlertaEntity().convertFromEntity(entidades.get(i)));
        }
        
        mostrarDatos(alertas.get(0));
    }
    
    private void findViews()
    {
        txtNom = (TextView) findViewById(R.id.txtNombre);
        txtDetalle = (TextView) findViewById(R.id.txtDetalle);
        estrellas = (ProgressBar) findViewById(R.id.estrellas);
        
        try
        {
        	mapa = (MapView) findViewById(R.id.mapa);
        } catch (Exception e) {
        	e.printStackTrace();
        }
	}

    private void mostrarDatos(AlertaEntity ae)
    {
    	estrellas.setVisibility(View.INVISIBLE);
    	estrellas.setProgress(ae.getNivel());
    	txtNom.setText(ae.getType() + " - " + ae.getNombre());
    	txtDetalle.setText(ae.getDetall());
    	mapa.getController().setCenter(ae.getPunto());
    	mapa.getController().setZoom(16);
    	
    	// Poner un PIN en el mapa
    	
    	ColorStateList pinCsl = createRandomColorStateList(false, ae.getNivel());
        ColorStateList dotCsl = createRandomColorStateList(true, ae.getNivel());
        BasicItemizedOverlay itemizedOverlay = new BasicItemizedOverlay(
        		new MapPinDrawable(getResources(), pinCsl, dotCsl));

    	
    	OverlayItem sitio = new OverlayItem(ae.getPunto(), ae.getNombre(), null);
    	
    	itemizedOverlay.addOverlay(sitio);
    	mapa.getOverlays().add(itemizedOverlay);
    }
    
    private String getStringFromRes(int id)
    {
    	return this.getResources().getString(id);
    }
    
    @Override
    protected void onDestroy() {
        try
        {
        	DataFramework.getInstance().close();
        } catch (Exception e){
        	e.printStackTrace();
        }
    	super.onDestroy();
    }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
    private class BasicItemizedOverlay extends ItemizedOverlay<OverlayItem> {

        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public BasicItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
        }

        public void addOverlay(OverlayItem overlay) {
            mOverlays.add(overlay);
            populate();
        }

        @Override
        protected OverlayItem createItem(int i) {
            return mOverlays.get(i);
        }

        @Override
        public int size() {
            return mOverlays.size();
        }

        @Override
        protected boolean onTap(int index) {
            return true;
        }

    }

    private ColorStateList createRandomColorStateList(Boolean dot, int i) {

        int[][] states = new int[2][];
        int[] colors = new int[2];

        int red = getResources().getColor(R.color.Red);
        int tred = getResources().getColor(R.color.TRed);
        int green = getResources().getColor(R.color.Green);
        int tgreen = getResources().getColor(R.color.TGreen);
        int orange = getResources().getColor(R.color.Orange);
        int torange = getResources().getColor(R.color.TOrange);
        
        if (dot)
        {
	        if (i > 3)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(tred, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = tred;
	        }
	        else if (i <= 1)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(green, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = green;
	        }
	        else
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(orange, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = orange;
	        }
        } else {
	        if (i > 3)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(red, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = red;
	        }
	        else if (i <= 1)
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(tgreen, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = tgreen;
	        }
	        else
	        {
	            states[0] = PRESSED_STATE;
	            colors[0] = addRGB(torange, -50);
	
	            states[1] = DrawableStateSet.EMPTY_STATE_SET;
	            colors[1] = torange;
	        }
        }
        
        
        
        return new ColorStateList(states, colors);
    }

    private static int addRGB(int color, int amount) {
        int r = constrain(Color.red(color) + amount, 0, 255);
        int g = constrain(Color.green(color) + amount, 0, 255);
        int b = constrain(Color.blue(color) + amount, 0, 255);
        return Color.rgb(r, g, b);
    }

    public static int constrain(int amount, int low, int high) {
        return amount < low ? low : (amount > high ? high : amount);
    }
}